package com.hjw.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.temp.SaTempUtil;
import cn.dev33.satoken.util.SaResult;
import com.hjw.entity.Comment;
import com.hjw.entity.User;
import com.hjw.service.CommentService;
import com.hjw.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @创建时间：2025-04-27
 * @文件名称：satoken_sobuk - com.hjw.controller
 */
@SaCheckRole("usr")
@Slf4j
@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;

    @PostMapping("addComment")
    public SaResult addComment(@RequestBody Map<String, Object> map){
        log.info("map:{}", map);
        if (map == null || map.isEmpty()){
            return SaResult.get(-200, "评论信息为空，评论失败", null);
        }
        if (!map.containsKey("bookid")){
            return SaResult.get(-200, "评论书籍编号不存在，评论失败", null);
        }
        if (!map.containsKey("content")){
            return SaResult.get(-200, "评论内容不存在，评论失败", null);
        }
        if (!map.containsKey("rate")){
            return SaResult.get(-200, "评论评分不存在，评论失败", null);
        }

        if (map.get("content").toString().length() > 200){
            return SaResult.get(-200, "评论内容不能超过200字，评论失败", null);
        }

        User user = userService.findById(StpUtil.getLoginIdAsInt());
        map.put("uid", user.getId());
        map.put("nickname", user.getNickname());
        map.put("img", user.getImg());
        log.info("评论信息：{}", map);

        int i = commentService.addComment(map);
        if (i == 0){
            return SaResult.get(-200, "评论失败", null);
        }
        Map<String, Object> m = new HashMap<>(1);
        m.put("bookid", map.get("bookid"));
        return SaResult.get(200, "评论成功", commentService.getComment(m));
    }

    /**
     * 用户获取评价信息
     */
    @GetMapping("/myComment")
    public SaResult myComment(){
        Map<String,Object> map = new HashMap<>();
        map.put("uid", StpUtil.getLoginIdAsInt());
        List<Comment> comments = commentService.getComment(map);
        if(comments == null || comments.isEmpty()){
            return SaResult.get(-200, "暂无评价信息", null);
        }
        return SaResult.get(200, "获取成功", comments);
    }

    @GetMapping("/delete/{id}")
    public SaResult deleteComment(@PathVariable("id") Integer id){
        int i = commentService.deleteComment(id);
        if (i == 0){
            return SaResult.get(-200, "删除失败", null);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("uid", StpUtil.getLoginIdAsInt());
        List<Comment> comments = commentService.getComment(map);
        return SaResult.get(200, "删除成功", comments);
    }





}
